package com.example.demo.config;

import com.example.demo.mapper.PermissionMap;
import com.example.demo.mapper.UserMapper;
import com.example.demo.util.SpringTool;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.web.filter.authz.PermissionsAuthorizationFilter;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;

public class PermissionFilter extends PermissionsAuthorizationFilter {
    @Override
    public boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) throws IOException {
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        String requestURI = httpServletRequest.getRequestURI();
        PermissionMap permissionMapper = (PermissionMap) SpringTool.getBean("permissionMap");
        UserMapper userMapper = (UserMapper) SpringTool.getBean("userMapper");
        String username = (String) SecurityUtils.getSubject().getPrincipal();
        String role = userMapper.getRole(username);
        if(role == null) {
            return false;
        }
        String[] rolesSplit = role.split("#");
        ArrayList<String> permissonList = new ArrayList<>();
        for (String roleItem : rolesSplit) {
            String permissins = permissionMapper.getPermissin(roleItem);
            String[] permissinSplit = permissins.split("#");
            for (String permissinItem : permissinSplit) {
                permissonList.add(permissinItem);
            }
        }
        String page = requestURI.split("/")[requestURI.split("/").length - 1];
        if (page.equalsIgnoreCase("watch")) {
            return checkPermissinIn("user:insert", permissonList);
        } else if (page.equalsIgnoreCase("insert")) {
            return checkPermissinIn("admin:insert", permissonList);
        }
        return false;
    }


    private boolean checkPermissinIn(String permission, ArrayList<String> permissonList) {
        for (String item : permissonList) {
            if (permission.equalsIgnoreCase(item)) {
                return true;
            }
        }
        return false;
    }

    @Override
    protected boolean onAccessDenied(ServletRequest request, ServletResponse response, Object mappedValue) throws Exception {
        System.out.println("你的权限不够");
        HttpServletResponse httpServletResponse = (HttpServletResponse) response;
        if(isAjax(request)){
            System.out.println("这是ajax请求");
            httpServletResponse.sendRedirect("/notPermission");
        }else {
            System.out.println("这不是ajax请求");
            httpServletResponse.sendRedirect("/page/common/notPermission");
        }
        return  false;
    }

    private boolean isAjax(ServletRequest request) {
        String header = ((HttpServletRequest) request).getHeader("X-Requested-With");
        if ("XMLHttpRequest".equalsIgnoreCase(header)) {
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }

}
